/// <reference path="../Bootstrap/js/bootstrap.min.js" />
/// <reference path="../Knockout/knockout-3.3.0.js" />
/// <reference path="jquery-1.11.2.min.js" />
/// <reference path="mars.utility.js" />
/// <reference path="../JQueryValidation/jquery.validate.min.js" />

var User = function (name, username, email) {
    this.Name = name;
    this.UserName = username.substring(username.indexOf("|") + 1);
    this.Email = email;
}

var DropDownItem = function (name, value) {
    this.Name = name;
    this.Value = value;
}

var MasterListsUtility = {
    
    GetBUList: function (userName) {
        var buList = new Array();
        var listItems = ListUtility.GetListItems(_spPageContextInfo.webAbsoluteUrl
            + "/_api/Web/Lists/GetByTitle('MARS_MS_BU')/Items/?filter=(MSUserName eq '" + userName + "')");
        if (listItems != null && listItems.length > 0) {
            for (var i = 0; i < listItems.length; i++) {
                buList.push(new DropDownItem(listItems[i].BU, listItems[i].BU));
            }
        }
        return buList;
    },

    GetModelOwnerList: function (userName) {
        var modelOwnerList = new Array();
        var listItems = ListUtility.GetListItems(_spPageContextInfo.webAbsoluteUrl
            + "/_api/Web/Lists/GetByTitle('MARS_MS_BU')/Items/?filter=(MSUserName eq '" + userName + "')");
        if (listItems != null && listItems.length > 0) {
            for (var i = 0; i < listItems.length; i++) {
                modelOwnerList.push(new DropDownItem(listItems[i].Model_x0020_Owner_x0020_Full_x00, listItems[i].MOUserName));
            }
        }        
        return modelOwnerList;
    },

    GetFindingTypeList: function () {
        var list = new Array();
        list.push(new DropDownItem("Remediation Strongly Advised", "RSA"));
        list.push(new DropDownItem("Remediation Advised", "RA"));
        list.push(new DropDownItem("Other Observation", "OOC"));
        return list;
    },
    
    GetStreamLeadList: function () {
        var list = [];
        var requestUri = _spPageContextInfo.webAbsoluteUrl +
               "/_api/web/Lists/GetByTitle('MARS_USER_ROLES')/Items?$filter=substringof('Stream Lead', Role)";
        var listItems = ListUtility.GetListItems(requestUri);
        if (listItems != null && listItems.length > 0) {
            for (var i = 0; i < listItems.length; i++) {
                var item = listItems[i];
                list.push(new DropDownItem(item.FullName, item.UserName));
            }
        }
        return list;
    },

    GetIVGHeadList: function () {
        var list = [];
        var requestUri = _spPageContextInfo.webAbsoluteUrl +
               "/_api/web/Lists/GetByTitle('MARS_USER_ROLES')/Items?$filter=substringof('IVG Head', Role)";
        var listItems = ListUtility.GetListItems(requestUri);
        if (listItems != null && listItems.length > 0) {
            for (var i = 0; i < listItems.length; i++) {
                var item = listItems[i];
                list.push(new DropDownItem(item.FullName, item.UserName));
            }
        }
        return list;
    },

    GetBUEList: function () {
        var list = [];
        var requestUri = _spPageContextInfo.webAbsoluteUrl +
               "/_api/web/Lists/GetByTitle('MARS_USER_ROLES')/Items?$filter=substringof('BUE', Role)";
        var listItems = ListUtility.GetListItems(requestUri);
        if (listItems != null && listItems.length > 0) {
            for (var i = 0; i < listItems.length; i++) {
                var item = listItems[i];
                list.push(new DropDownItem(item.FullName, item.UserName));
            }
        }
        return list;
    },

    GetMROList: function () {
        var list = [];
        var requestUri = _spPageContextInfo.webAbsoluteUrl +
               "/_api/web/Lists/GetByTitle('MARS_USER_ROLES')/Items?$filter=substringof('MRO\\CRO', Role)";
        var listItems = ListUtility.GetListItems(requestUri);
        if (listItems != null && listItems.length > 0) {
            for (var i = 0; i < listItems.length; i++) {
                var item = listItems[i];
                list.push(new DropDownItem(item.FullName, item.UserName));
            }
        }
        return list;
    },

    GetPhase1ProgressList: function () {
        var list = MasterListsUtility.GetProgressList();
        list.push(new DropDownItem("Document Rejected", "Document Rejected"));
        return list;
    },

    GetProgressList: function () {
        var list = new Array();
        list.push(new DropDownItem("Not Started", "Not Started"));
        list.push(new DropDownItem("In Progress", "In Progres"));
        list.push(new DropDownItem("Completed", "Completed"));
        return list;
    },

    GetValidationTrackerList: function () {
        var list = new Array();
        list.push(new DropDownItem("Under IVG Review", "Under IVG Review"));
        list.push(new DropDownItem("Sent to BU", "Sent to BU"));
        return list;
    },

    GetConfigValueArray: function (configName) {
        var list = new Array();
        var configValues = MasterListsUtility.GetConfigValue(configName);
        if (configValues != null) {
            var temp = configValues.split(",");
            for (var i = 0; i < temp.length; i++) {
                list.push(new DropDownItem($.trim(temp[i]), $.trim(temp[i])));
            }
        }
        return list;
    },

    GetConfigValue: function (configKey) {
        var configValue = null;
        var call = $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('MARS_CONFIG')/Items?$select=Title,Value&$filter=(Title eq '" + configKey + "')",
            async: false,
            type: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            }
        });
        call.done(function (data, textStatus, jqXHR) {
            var configValues = data.d.results;
            if (configValues.length > 0) {
                configValue = configValues[0].Value;
            }
        });
        call.fail(function (jqXHR, textStatus, errorThrown) {
            alert("Error: " + errorThrown);
        });
        return configValue;
    },

    GetLeadValidatorList: function () {
        var list = [];
        var requestUri = _spPageContextInfo.webAbsoluteUrl +
               "/_api/web/Lists/GetByTitle('MARS_USER_ROLES')/Items?$filter=((Role eq 'IVG') or substringof('Stream Lead', Role))";
        var listItems = ListUtility.GetListItems(requestUri);
        if (listItems != null && listItems.length > 0) {
            for (var i = 0; i < listItems.length; i++) {
                var item = listItems[i];
                list.push(new DropDownItem(item.FullName, item.UserName));
            }
        }
        return list;
    }
}
